package com.lxl.service.impl;

import com.lxl.common.CommonUtil;
import com.lxl.common.MyConstant;
import com.lxl.mapper.DepartmentMapper;
import com.lxl.pojo.Department;
import com.lxl.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * todo: 返回ResultEntity 可用于中途处理异常情况/ 在controller中判断？？
 * @Date 2021/11/25 17:26
 * @Created by LXLDEMO
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAll() {
        List<Department> all = departmentMapper.getAll();
        if (CommonUtil.isEmpty(all)){
            all = MyConstant.LIST_DEFAULT_VALUE;
        }
        return all;
    }

    @Override
    public Department getById(Map<String, Object> param) {
        Department department = departmentMapper.getDepartmentById(param);
        if (CommonUtil.isEmpty(department)){
            department = new Department(-1,"null");
        }
        return department;
    }

    @Override
    public String delById(Map<String, Object> param) {
        Integer integer = null;
        try {
            integer = departmentMapper.delDepartmentById(param);
        } catch (Exception e) {
            e.printStackTrace();
            return "出现异常";
        }
        return integer == 1 ? "删除成功":"删除失败";
    }

    @Override
    public String addItem(Map<String, Object> param) {

        Integer integer = null;
        try {
            integer = departmentMapper.addDepartment(param);
        } catch (Exception e) {
            e.printStackTrace();
            return "出现异常";
        }
        return integer == 1 ? "新增成功":"新增失败";
    }

    @Override
    public String updateItem(Map<String, Object> param) {
        Integer integer = null;
        try {
            integer = departmentMapper.updateDepartmentById(param);
        } catch (Exception e) {
            e.printStackTrace();
            return "出现异常";
        }
        return integer == 1 ? "更新成功":"更新失败";
    }
}
